<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="团课名称">
        <el-input v-model="league.name"/>
      </el-form-item>
      <el-form-item label="团课时长">
        <el-input v-model="league.time"/>
      </el-form-item>
      <el-form-item label="讲师排序">
        <el-input-number v-model="league.sort" controls-position="right"/>
      </el-form-item>
      <el-form-item label="教练">
        <el-select v-model="league.coachId" clearable placeholder="请选择">
          <el-option
            v-for="coach in coachList"
            :key="coach.id"
            :label="coach.name"
            :value="coach.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课时费">
        <el-input v-model="league.cost"/>
      </el-form-item>
      <el-form-item label="可预约人数">
        <el-input v-model="league.bookCount"/>
      </el-form-item>
      <el-form-item label="团课介绍">
        <el-input v-model="league.introduction" />
      </el-form-item >
      <el-form-item label="团课标签">
        <el-tag
          :key="tag"
          v-for="tag in dynamicTags"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)">
          {{tag}}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-if="inputVisible"
          v-model="inputValue"
          ref="saveTagInput"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        >
        </el-input>
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
      </el-form-item >
      <!-- 讲师头像：TODO -->
      <el-form-item label="讲师头像">
        <el-upload
          name="file"
          class="avatar-uploader"
          action="http://localhost:8080/upload/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="league.img" :src="'http://rj0387v46.hn-bkt.clouddn.com/' + league.img" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item >
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="handleAddOrEdit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import league from "../../api/league";
import coach from "../../api/coach";
  export default {
    data(){
      return{
        league: {
          img: "",
          label: ""
        },
        dynamicTags: [],
        inputVisible: false,
        inputValue: '',
        saveBtnDisabled: false,
        coachList: [],
      }
    },
    created(){
      if(this.$route.params && this.$route.params.id){
        this.queryById(this.$route.params.id);
      }
      this.getName()
    },
    methods: {
      // 获取教练名称
      getName(){
        coach.getName().then(res => {
          this.coachList = res.data
        })
      },
      handleAddOrEdit(){
        this.saveBtnDisabled = true;
        if(this.$route.params && this.$route.params.id){
          league.edit(this.league, this.dynamicTags).then(res => {
            this.$message.success(res.message);
            this.$router.push({ path: '/league' })
          })
        }else{
          league.add(this.league, this.dynamicTags).then(res => {
            this.$message.success(res.message);
            this.$router.push({ path: '/league' })
          })
        }
        this.saveBtnDisabled = true;
      },
      queryById(id){
        league.getById(id).then(res => {
          this.league = res.data.league;
          this.dynamicTags = res.data.dynamicTags
        })
      },
      handleAvatarSuccess(res, file) {
        this.league.img = res.data
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      handleClose(tag) {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      },

      showInput() {
        this.inputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },

      handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = '';
      }
    }
  }
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
